<template>
  <div class="mod-card__dealer">
    <el-form :inline="true" :model="state.dataForm" @keyup.enter="state.getDataList()">
      <div class="top-search_form">
        <div class="ttop-search_form_input">
          <el-form-item :label="$t('leave.name')">
            <el-input v-model="state.dataForm.name" :placeholder="$t('input')" clearable></el-input>
          </el-form-item>
        </div>
        <div class="top-search_form_btn">
          <el-form-item>
            <el-button @click="state.getDataList()">{{ $t("reset") }}</el-button>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="state.getDataList()">{{ $t("query") }}</el-button>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="addOrUpdateHandle()">{{ $t("add") }}</el-button>
          </el-form-item>
          <el-form-item>
            <el-button type="danger" @click="state.deleteHandle()">{{ $t("deleteBatch") }}</el-button>
          </el-form-item>
        </div>
      </div>
    </el-form>
    <el-table v-loading="state.dataListLoading" :data="state.dataList" border
      @selection-change="state.dataListSelectionChangeHandle" style="width: 100%">
      <el-table-column type="selection" header-align="center" align="center" width="50"></el-table-column>
      <el-table-column prop="name" :label="$t('leave.name')" header-align="center" align="center"
        width="120"></el-table-column>
      <el-table-column prop="className" :label="$t('leave.className')" header-align="center" align="center"
        width="120"></el-table-column>
      <el-table-column prop="gender" :label="$t('leave.gender')" header-align="center" align="center" width="120">
        <template v-slot="scope">
          {{ GenderMap[scope.row.gender] }}
        </template>
      </el-table-column>
      <el-table-column prop="typeId" :label="$t('leave.typeName')" header-align="center" align="center" width="120">
        <template v-slot="scope">
          {{ leaveTypeList.find((d) => d.id === scope.row.typeId)?.name }}
        </template>
      </el-table-column>
      <el-table-column prop="canReturnDormitory" :label="$t('leave.canReturnDormitory')" header-align="center"
        align="center" width="120">
        <template v-slot="scope">
          <el-switch v-model="scope.row.canReturnDormitory" :active-value="1" :inactive-value="0" inline-prompt
            active-text="是" inactive-text="否" disabled />
        </template>
      </el-table-column>
      <el-table-column prop="canReturnSchool" :label="$t('leave.canReturnSchool')" header-align="center" align="center"
        width="120">
        <template v-slot="scope">
          <el-switch v-model="scope.row.canReturnSchool" :active-value="1" :inactive-value="0" inline-prompt
            active-text="是" inactive-text="否" disabled />
        </template>
      </el-table-column>
      <el-table-column prop="isInSchool" :label="$t('leave.isInSchool')" header-align="center" align="center"
        width="120">
        <template v-slot="scope">
          <el-switch v-model="scope.row.isInSchool" :active-value="1" :inactive-value="0" inline-prompt active-text="是"
            inactive-text="否" disabled />
        </template>
      </el-table-column>
      <el-table-column prop="certificate" :label="$t('leave.certificate')" header-align="center" align="center"
        width="120"></el-table-column>
      <el-table-column prop="reason" :label="$t('leave.reason')" header-align="center" align="center"
        width="120"></el-table-column>
      <el-table-column prop="effectiveTime" :label="$t('leave.effectiveTime')" header-align="center" align="center"
        width="180"></el-table-column>
      <el-table-column prop="expiryTime" :label="$t('leave.expiryTime')" header-align="center" align="center"
        width="180"></el-table-column>
      <el-table-column prop="createDate" :label="$t('leave.createDate')" header-align="center" align="center"
        width="180"></el-table-column>
      <el-table-column :label="$t('handle')" fixed="right" header-align="center" align="center" width="200">
        <template v-slot="scope">
          <el-button type="primary" link @click="issueDetailHandle(scope.row.id)">下发详情</el-button>
          <el-button type="primary" link @click="addOrUpdateHandle(scope.row.id)">{{ $t("update") }}</el-button>
          <el-button type="primary" link @click="state.deleteHandle(scope.row.id)">{{ $t("delete") }}</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination :current-page="state.page" :page-sizes="[10, 20, 50, 100]" :page-size="state.limit"
      :total="state.total" layout="total, sizes, prev, pager, next, jumper" @size-change="state.pageSizeChangeHandle"
      @current-change="state.pageCurrentChangeHandle"> </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update ref="addOrUpdateRef" @refreshDataList="state.getDataList"></add-or-update>
    <issue-detail ref="issueDetailRef" @refreshDataList="state.getDataList"></issue-detail>
  </div>
</template>

<script lang="ts" setup>
import useView from "@/hooks/useView";
import baseService from "@/service/baseService";
import { onMounted, reactive, ref, toRefs } from "vue";
import AddOrUpdate from "./leave-add-or-update.vue";
import issueDetail from "./issue-detail.vue";
import { GenderMap } from './contant';

const view = reactive({
  getDataListURL: "/leave/info/page",
  getDataListIsPage: true,
  deleteURL: "/leave/info",
  deleteIsBatch: true,
  dataForm: {
    name: "",
    status: null,
  }
});

const state = reactive({ ...useView(view), ...toRefs(view) });

const addOrUpdateRef = ref();
const addOrUpdateHandle = (id?: number) => {
  addOrUpdateRef.value.init(id);
};

const issueDetailRef = ref();
const issueDetailHandle = (id?: number) => {
  issueDetailRef.value.init(id);
};

// 获取请假类型列表
const leaveTypeList = ref<any[]>([]);
const getLeaveTypeList = () => {
  baseService.get("/leave/type/list").then((res) => {
    leaveTypeList.value = res.data;
  });
};

onMounted(() => {
  getLeaveTypeList();
});
</script>
